// app.js
App({
  onLaunch() {
    const {
      top,
      width,
      height,
      right
    } = wx.getMenuButtonBoundingClientRect();

    
    this.globalData = {
      ...this.globalData,
      top,
      width,
      height,
      right
    }
    wx.getStorage({
      key: "_userInfo",
      success: ({
        data
      }) => {
        console.log('_userInfo', data)
        if (data) {
          this.globalData.userInfo = data
        } else {
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      },
      fail: () => {
        // wx.navigateTo({
        //   url: '/pages/login/login'
        // })
      }
    })
    this.overShare();
    // this.msgLoading()
  },

  overShare() {
    // const app = my.getApp();
    // console.log(app);

    wx.onAppRoute(function () {
      let pages = getCurrentPages(),
        view = pages[pages.length - 1],
        data;
      if (view) {
        data = view.data;
        console.log(data);
        wx.showShareMenu({
          withShareTicket: true,
          menus: ['shareAppMessage', 'shareTimeline']
        })
        // 判断是否需要重写分享方法
        if (!data.isOverShare) {
          data.isOverShare = true;
          view.onShareTimeline = function () {
            return {
              title: '上达街'
            };
          }
          view.onShareAppMessage = function () {
            return {
              title: '上达街'
            };
          }
        }
      }
    })
  },



  msgLoading(){
    wx.showLoading({
      title: '加载中...',
      mask:true,
      icon:'none'
    })
  },
  toShowToast(){
    wx.showToast({
      title: '功能正在开发中敬请期待',
      icon:'none'
    })
  },
  toUrl(e) {
    console.log(e);
    let {
      url,
      replace = false,
      go,
      check = false,
      type = '',
      listId = '',
      typeurl = 1,
      id = '',
    } = e.currentTarget.dataset;


    if (check) { //当前路由需要检测登录才能放行
      let {
        token
      } = getApp().globalData;
      if (token == null || !token) {
        wx.redirectTo({
          url: '/pages/login/login',
        });
        return wx.showToast({
          title: '请先登录',
          icon: "none"
        });
      }
    }
    console.log(typeurl);
    if (replace && url) {
      wx.redirectTo({
        url
      })
    } else if (go != undefined && go < 0) {
      go = Number(go)
      wx.navigateBack({
        delta: -go
      })
    } else if (url) {
      console.log(url);
      if (typeurl == 1) {
        let webview = url.indexOf('weixin')
        if (webview != -1) {
          wx.navigateTo({
            url: "/pages/webview/webview?url=" + encodeURIComponent(url)
          })
        } else {
          // let url = ** * ; //需要跳转的地址
          let pages = getCurrentPages(); //页面栈
          console.log(pages);
          let is_link = 1; //判断getCurrentPages是否有重复的地址
          let iindex = ''; //获取重复地址的下标
          for (let i = 0; i < pages.length; i++) {
            let is_url = "/" + pages[i]?.route // 拼接“/”成为一个绝对路径
            if (url == is_url) {
              is_link++;
              iindex = i;
            }
          }
          //判断 地址有重复的话 就使用后退 delta后退多级
          if (is_link == 2) {
            wx.navigateBack({
              delta: pages.length - iindex - 1
            })
          } else {
            console.log(url);
            //getCurrentPages没到达10级就可以使用navigateTo跳转  
            if (pages.length != 10) {
              wx.navigateTo({
                url: `${url}?type=${type}&listId=${listId}`
              })
            } else {
              //有超过的话 使用redirectTo 关闭当前页进行跳转
              wx.redirectTo({
                url: `${url}?type=${type}&listId=${listId}`
              })
            }
          }
          


        }
      } else {
        wx.navigateTo({
          url: `/pages/login/agreement/agreement?title=栏目详情&ids=${id}`
        })
      }
    } else {
      if (typeurl == 2) {
        wx.navigateTo({
          url: `/pages/login/agreement/agreement?title=栏目详情&ids=${id}`
        })
      }
    }
  },
  onShow(){
    console.log('来的');
  },
  onHide() {
    wx.removeStorage({
      key: 'time',
    });
  },
  // onShow(){
  //   console.log('被显示了');
  // },
  globalData: {
    // baseUrl: 'https://www.qwcfcenter.com/',
    baseUrl: 'https://shangda.doing.net.cn/',

    userInfo: null,
    baseInfo: {},
    token: null,
    xToken: null
  }
})